/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package visual.Seguridad;

import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author luis
 */
public class encriptado {
    private String key = "4d89g13j4j91j27c582ji69373y788r6";
//    private String patronBusqueda = "ABCDEFGHIJKLMNÑOPQRSTVWXYZabcdefghijklmnñopqrstvwxyz1234567890";
//    private String patronEncripta = "023456789”¢€→¶↓“»←«°¡¿{}[]¬|@·~½¬`~!@#$%^&*()_+=\\|\"':;?/>.<,";
//
//    public String encriptaCadena(String cadena){
//        String resultado = "";
//
//        for (int i = 0; i < cadena.length(); i++)
//            resultado+= encriptaCaracter(cadena.substring(i, i+1),cadena.length(),i);
//
//
//
//        return resultado;
//    }
//
//
//    private String encriptaCaracter(String caracter, int variable, int indice){
//
//        String caracterEncriptado = "";
//        int auxIndice = 0;
//
//        if (this.patronBusqueda.indexOf(caracter)!=-1){
//            auxIndice = (this.patronBusqueda.indexOf(caracter));
//            return this.patronEncripta.substring(auxIndice, auxIndice+1);
//        }
//        return caracter;
//    }
//
//
//    public String desEncriptaCadena(String cadena){
//
//        String resultado = "";
//
//        for (int i = 0; i < cadena.length(); i++)
//            resultado+= desEncriptaCaracter(cadena.substring(i, i+1),cadena.length(),i);
//
//        return resultado;
//    }
//
//
//    private String desEncriptaCaracter(String caracter, int variable, int indice){
//
//        int auxIndice = 0;
//
//        if (this.patronEncripta.indexOf(caracter) != -1){
//            auxIndice = (this.patronEncripta.indexOf(caracter));
//            return this.patronBusqueda.substring(auxIndice, auxIndice+1);
//        }
//        return caracter;
//    }


    Cipher encrypt;
    Cipher decrypt;
    
    String keyTxt;
  

    public encriptado(){
        keyTxt = "4d89g13j4j91j27c582ji69373y788r6";
        try{
            encrypt = Cipher.getInstance("DES");
            decrypt = Cipher.getInstance("DES");

            
            KeySpec ks = new DESKeySpec(keyTxt.getBytes("UTF8"));
            SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
            SecretKey ky = kf.generateSecret(ks);
            encrypt.init(Cipher.ENCRYPT_MODE, ky);
            decrypt.init(Cipher.DECRYPT_MODE, ky);

        }catch(InvalidKeySpecException ex){
        }catch(UnsupportedEncodingException ex){
        }catch(InvalidKeyException ex){
        }catch(NoSuchAlgorithmException ex){
        }catch(NoSuchPaddingException ex){
        }
      }

      public String encriptar(String str){
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = encrypt.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }

    public String desencriptar(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

            // Decrypt
            byte[] utf8 = decrypt.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }


}
